home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cdgtst / hyperlin.cpp < prev    next >
C/C++ Source or Header  |  1997-11-24  |  9KB  |  304 lines

  1. // HyperLink.cpp : implementation file
  2. //
  3. // HyperLink static control. Will open the default browser with the given URL
  4. // when the user clicks on the link.
  5. //
  6. // Copyright Chris Maunder, 1997
  7. // Feel free to use and distribute.
  8. //
  9. // Thanks to Pσl K. T°nder for auto-size and window caption changes.
  10. //
  11. // "GotoURL" function by Stuart Patterson
  12. // As seen in the August, 1997 Windows Developer's Journal.
  13. // Copyright 1997 by Miller Freeman, Inc.
  14. // All rights reserved.
  15. // Modified by Chris Maunder to use TCHARs instead of chars.
  16.  
  17. #include "stdafx.h"
  18. #include "HyperLink.h"
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. #define TOOLTIP_ID 1
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CHyperLink
  30.  
  31. CHyperLink::CHyperLink()
  32. {
  33.     m_crLinkColour      = RGB(0, 0, 238);     // Blue
  34.     m_crVisitedColour   = RGB(85, 26, 139);   // Purple
  35.     m_bVisited          = FALSE;              // Hasn't been visited yet.
  36.     m_bUnderline        = TRUE;               // Underline the link?
  37.     m_bAdjustToFit      = TRUE;               // Resize the window to fit the text?
  38.     m_strURL.Empty();
  39. }
  40.  
  41. CHyperLink::~CHyperLink()
  42. {
  43.     if ((HFONT)m_Font)
  44.         m_Font.DeleteObject();
  45. }
  46.  
  47.  
  48. BEGIN_MESSAGE_MAP(CHyperLink, CStatic)
  49.     //{{AFX_MSG_MAP(CHyperLink)
  50.     ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
  51.     ON_WM_CTLCOLOR_REFLECT()
  52.     ON_WM_SETCURSOR()
  53.     //}}AFX_MSG_MAP
  54. END_MESSAGE_MAP()
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CHyperLink message handlers
  58.  
  59. BOOL CHyperLink::PreTranslateMessage(MSG* pMsg) 
  60. {
  61.     m_ToolTip.RelayEvent(pMsg);
  62.     return CStatic::PreTranslateMessage(pMsg);
  63. }
  64.  
  65. HBRUSH CHyperLink::CtlColor(CDC* pDC, UINT nCtlColor) 
  66. {
  67.     ASSERT(nCtlColor == CTLCOLOR_STATIC);
  68.  
  69.     // Set the approriate colour
  70.     if (m_bVisited)
  71.         pDC->SetTextColor(m_crVisitedColour);
  72.     else
  73.         pDC->SetTextColor(m_crLinkColour);
  74.  
  75.     // Set underline font if required
  76.     if (m_bUnderline) {
  77.  
  78.         if (!(HFONT)m_Font) {            // Create font only once.
  79.             LOGFONT lf;
  80.             GetFont()->GetLogFont(&lf);
  81.             lf.lfUnderline = TRUE;
  82.             m_Font.CreateFontIndirect(&lf);
  83.         }
  84.  
  85.         pDC->SelectObject(&m_Font);
  86.     }
  87.  
  88.     // transparent text.
  89.     pDC->SetBkMode(TRANSPARENT);
  90.     return (HBRUSH)GetStockObject(NULL_BRUSH);
  91. }
  92.  
  93. BOOL CHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/) 
  94. {
  95.     ::SetCursor(m_hLinkCursor);
  96.     return TRUE;
  97. }
  98.  
  99. void CHyperLink::PreSubclassWindow() 
  100. {
  101.     // We want to get mouse clicks via STN_CLICKED
  102.     DWORD dwStyle = GetStyle();
  103.     ::SetWindowLong(m_hWnd, GWL_STYLE, dwStyle | SS_NOTIFY);
  104.     
  105.     // Set the URL as the window text
  106.     if (m_strURL.IsEmpty())
  107.         GetWindowText(m_strURL);
  108.  
  109.     // Check that the window text isn't empty. If it is, set it as the URL.
  110.     CString strWndText;
  111.     GetWindowText(strWndText);
  112.     if (strWndText.IsEmpty()) {
  113.         ASSERT(!m_strURL.IsEmpty());    // Window and URL both empty. DUH!
  114.         SetWindowText(m_strURL);
  115.     }
  116.  
  117.     // Adjust size of window to fit URL if necessary
  118.     PositionWindow();        
  119.  
  120.     // Load up the (standard) hyperlink cursor
  121.     m_hLinkCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  122.  
  123.     // Create the tooltip
  124.     CRect rect; 
  125.     GetClientRect(rect);
  126.     m_ToolTip.Create(this);
  127.     m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
  128.  
  129.     CStatic::PreSubclassWindow();
  130. }
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CHyperLink operations
  134.  
  135. void CHyperLink::SetURL(CString strURL)
  136. {
  137.     m_strURL = strURL;
  138.     PositionWindow();
  139.     m_ToolTip.UpdateTipText(strURL, this, TOOLTIP_ID);
  140. }
  141.  
  142. void CHyperLink::SetColours(COLORREF crLinkColour, COLORREF crVisitedColour) 
  143.     m_crLinkColour    = crLinkColour; 
  144.     m_crVisitedColour = crVisitedColour; 
  145.     Invalidate(); 
  146. }
  147.  
  148. void CHyperLink::SetVisited(BOOL bVisited /* = TRUE */) 
  149.     m_bVisited = bVisited; 
  150.     Invalidate(); 
  151. }
  152.  
  153. void CHyperLink::SetUnderline(BOOL bUnderline /* = TRUE */)
  154. {
  155.     m_bUnderline = bUnderline; 
  156.     Invalidate(); 
  157. }
  158.  
  159. void CHyperLink::SetAutoSize(BOOL bAutoSize /* = TRUE */)
  160. {
  161.     m_bAdjustToFit = bAutoSize;
  162.     PositionWindow();
  163. }
  164.  
  165.  
  166. // Move and resize the window so that the window is the same size
  167. // as the hyperlink text. This stops the hyperlink cursor being active
  168. // when it is not directly over the text. If the text is left justified
  169. // then the window is merely shrunk, but if it is centred or right
  170. // justified then the window will have to be moved as well.
  171. //
  172. // Suggested by Pσl K. T°nder 
  173.  
  174. void CHyperLink::PositionWindow()
  175. {
  176.     if (!m_bAdjustToFit) return;
  177.  
  178.     // Get the current window position
  179.     CRect rect;
  180.     GetWindowRect(rect);
  181.  
  182.     CWnd* pParent = GetParent();
  183.     if (pParent)
  184.         pParent->ScreenToClient(rect);
  185.  
  186.     // Get the size of the window text
  187.     CString strWndText;
  188.     GetWindowText(strWndText);
  189.  
  190.     CDC* pDC = GetDC();
  191.     CSize Extent = pDC->GetTextExtent(strWndText);
  192.     ReleaseDC(pDC);
  193.  
  194.     // Get the text justification via the window style
  195.     DWORD dwStyle = GetStyle();
  196.  
  197.     // Recalc the window size and position based on the text justification
  198.     if (dwStyle & SS_CENTERIMAGE)
  199.         rect.DeflateRect(0, (rect.Height() - Extent.cy)/2);
  200.     else
  201.         rect.bottom = rect.top + Extent.cy;
  202.  
  203.     if (dwStyle & SS_CENTER)   
  204.         rect.DeflateRect((rect.Width() - Extent.cx)/2, 0);
  205.     else if (dwStyle & SS_RIGHT) 
  206.         rect.left  = rect.right - Extent.cx;
  207.     else // SS_LEFT = 0, so we can't test for it explicitly 
  208.         rect.right = rect.left + Extent.cx;
  209.  
  210.     // Move the window
  211.     SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
  212. }
  213.  
  214. /////////////////////////////////////////////////////////////////////////////
  215. // CHyperLink implementation
  216.  
  217. void CHyperLink::OnClicked()
  218. {
  219.     int result = (int)GotoURL(m_strURL, SW_SHOW);
  220.     m_bVisited = (result > HINSTANCE_ERROR);
  221.     if (!m_bVisited) {
  222.         MessageBeep(MB_ICONEXCLAMATION);    // Unable to follow link
  223.         ReportError(result);
  224.     } else 
  225.         SetVisited();                        // Repaint to show visited colour
  226. }
  227.  
  228. LONG CHyperLink::GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
  229. {
  230.     HKEY hkey;
  231.     LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
  232.  
  233.     if (retval == ERROR_SUCCESS) {
  234.         long datasize = MAX_PATH;
  235.         TCHAR data[MAX_PATH];
  236.         RegQueryValue(hkey, NULL, data, &datasize);
  237.         lstrcpy(retdata,data);
  238.         RegCloseKey(hkey);
  239.     }
  240.  
  241.     return retval;
  242. }
  243.  
  244. void CHyperLink::ReportError(int nError)
  245. {
  246.     CString str;
  247.     switch (nError) {
  248.         case 0:                       str = "The operating system is out\nof memory or resources."; break;
  249.         case SE_ERR_PNF:              str = "The specified path was not found."; break;
  250.         case SE_ERR_FNF:              str = "The specified file was not found."; break;
  251.         case ERROR_BAD_FORMAT:        str = "The .EXE file is invalid\n(non-Win32 .EXE or error in .EXE image)."; break;
  252.         case SE_ERR_ACCESSDENIED:     str = "The operating system denied\naccess to the specified file."; break;
  253.         case SE_ERR_ASSOCINCOMPLETE:  str = "The filename association is\nincomplete or invalid."; break;
  254.         case SE_ERR_DDEBUSY:          str = "The DDE transaction could not\nbe completed because other DDE transactions\nwere being processed."; break;
  255.         case SE_ERR_DDEFAIL:          str = "The DDE transaction failed."; break;
  256.         case SE_ERR_DDETIMEOUT:       str = "The DDE transaction could not\nbe completed because the request timed out."; break;
  257.         case SE_ERR_DLLNOTFOUND:      str = "The specified dynamic-link library was not found."; break;
  258.         case SE_ERR_NOASSOC:          str = "There is no application associated\nwith the given filename extension."; break;
  259.         case SE_ERR_OOM:              str = "There was not enough memory to complete the operation."; break;
  260.         case SE_ERR_SHARE:            str = "A sharing violation occurred. ";
  261.         default:                      str.Format("Unknown Error (%d) occurred.", nError); break;
  262.     }
  263.     str = "Unable to open hyperlink:\n\n" + str;
  264.     AfxMessageBox(str, MB_ICONEXCLAMATION | MB_OK);
  265. }
  266.  
  267. HINSTANCE CHyperLink::GotoURL(LPCTSTR url, int showcmd)
  268. {
  269.     TCHAR key[MAX_PATH + MAX_PATH];
  270.  
  271.     // First try ShellExecute()
  272.     HINSTANCE result = ShellExecute(NULL, _T("open"), url, NUL